#include "vtor_led.h"
//#include "vtor_string.h"

#ifdef __VTOR_LED__

void VtorPwm_Run(VtorPwm* pwm, int8_t incTick)
{
	// 呼吸灯模式
	// 更新模拟定时器cnt
	pwm->cnt += incTick;
	pwm->cnt %= pwm->arr;

	uint8_t curState = pwm->cnt < pwm->ccr;
	if(pwm->lastState != curState)
	{
		pwm->lastState = curState;
		VtorPwm_WritePin(pwm, curState);
	}
}

void VtorPwm_SetCcr(VtorPwm* pwm, int16_t ccr)
{
	pwm->ccr = ccr;
}


void VtorBlink_Run(VtorBlink* blink, int16_t incTick)
{
	blink->cycleCnt += incTick;

	if(blink->cycleCnt > blink->cycleArr)
	{
		// 如果整个闪烁周期结束了，清空闪烁次数，清空当前tick
		blink->cycleCnt = 0;
		blink->repeatCnt = 0;
		blink->pwmCnt = 0;
	}
	
	blink->pwmCnt += incTick;
	if(blink->pwmCnt > blink->pwmArr)
	{
		blink->pwmCnt = 0;
		blink->repeatCnt ++; // 单个亮灭周期已经到了
	}
	// 只有在有效的亮灭情况下，
	if(blink->repeatCnt < blink->repeatCcr)
	{
		uint8_t curState = blink->pwmCnt < blink->pwmCcr;
		if(blink->lastState != curState)
		{
			blink->lastState = curState;
			VtorBlink_WritePin(blink, curState);
		}
	}
}

void VtorBlink_SetCcr(VtorBlink* blink, int16_t ccr)
{
	blink->repeatCcr = ccr; // 设置闪烁次数
}

void VtorLed_SetMode(VtorLed* led, int8_t mode)
{
	led->mode = mode;
	// 预设几种模式
	int16_t defaultVtorLedParamArray[][3] = 
	{
		0,0,0,
		100,100,100,
		3000,1500,3000,

		200,100,200,
		200,100,400,
		200,100,600,

		500,400,500,
		500,400,1000,
		500,400,1500,

		200,100,3000,
		500,200,3000,
		1000,900,3000,

		24,0,3000,
		24,0,1000,
		24,0,300,
	};
	// 配置参数
	VtorLed_SetParams(led,
		defaultVtorLedParamArray[mode][0],
		defaultVtorLedParamArray[mode][1],
		defaultVtorLedParamArray[mode][2]);
}

void VtorLed_SetParams(VtorLed* led, int16_t arr, int16_t ccr, int16_t cycleTime)
{

//	int8_t str[128];
//	VtorString_Clear(str);
//	VtorString_AppendString(str, "VtorLed_SetParams(");
//	VtorString_AppendInt(str, arr);
//	VtorString_AppendString(str, ", ");
//	VtorString_AppendInt(str, ccr);
//	VtorString_AppendString(str, ", ");
//	VtorString_AppendInt(str, cycleTime);
//	VtorString_AppendString(str, ")\r\n");
//	VtorString_Print(str);

	led->pwmCnt = 0;
	led->holdCnt = 0;

	led->pwmArr = arr;
	led->pwmCcr = ccr;
	if(led->mode < 12)
	{
		// 闪烁灯情况下，根据cycleTime计算3000ms内有多少个arr
		led->holdCcr = cycleTime;
	}
	else
	{
		// 呼吸灯情况下，cycleTime代表总呼吸时长，
		// 根据cycleTime计算每一级亮度持续时间
		led->holdCcr = cycleTime / ((led->pwmArr - VTOR_LED_CONFIG_BREATH_MAX_BRIGHTNESS_DIFF) * 2 + 1) - 1;
	}
}


void VtorLed_Run(VtorLed* led, int8_t tickInc)
{
	// 呼吸灯模式
	// 更新模拟定时器cnt
	led->pwmCnt += tickInc;
	led->pwmCnt %= led->pwmArr;

	if(led->mode < 12)
	{
		// 单闪，双闪等模式
		// holdCcr 决定单周期内总有效控制时长
		// 非常建议holdCcr刚好 小于等于 整数倍pwmArr
		led->holdCnt += tickInc;
		if(led->holdCnt < led->holdCcr)
		{
			led->curState = led->pwmCnt < led->pwmCcr;
		}
		if(led->holdCnt >= VTOR_LED_CONFIG_BLINK_TOTAL_TIME)
		{
			led->holdCnt = 0;
			led->pwmCnt = 0;
		}
	}
	else
	{
		// 每级亮度维持时间
		led->holdCnt += tickInc;
		if(led->holdCnt >= led->holdCcr) // ccr当arr用，达到时就清零
		{
			led->holdCnt = 0;
			// 更新pwm期望占空比
			led->pwmCcr += led->pwmCcrInc;
			if(led->pwmCcr >= led->pwmArr - VTOR_LED_CONFIG_BREATH_MAX_BRIGHTNESS_DIFF)
			{
				led->pwmCcrInc = -1;
			}
			else if(led->pwmCcr <= 0)
			{
				led->pwmCcrInc = 1;
			}
		}
		led->curState = led->pwmCnt < led->pwmCcr;
	}
	// 亮灯 灭灯
	if(led->lastState != led->curState)
	{
		led->lastState = led->curState;
		VtorLed_WritePin(led, led->curState);
	}
}

#endif // __VTOR_LED__
